package com.orderplus.opshop.synces.service.elasticsearch.impl;

import com.orderplus.opshop.common.util.ValueUtil;
import com.orderplus.opshop.core.data.request.RequestParam;
import com.orderplus.opshop.core.data.response.ResponseData;
import com.orderplus.opshop.core.template.TransTemplate;
import com.orderplus.opshop.synces.bean.ProductDetailModel;
import com.orderplus.opshop.synces.constant.EsConstant;
import com.orderplus.opshop.synces.request.OpEsIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * author:maihj 2018.9.21
 * <p>
 * OpEsIndexRequest     			原来的请求
 * Boolean     						原来的响应
 * BulkRequest   					转换后的请求
 * ResponseData<Boolean>, Boolean> 	转换后的响应
 */
@Service
public class OpUpdateByQueryService extends TransTemplate<OpEsIndexRequest, Boolean, UpdateByQueryRequest, ResponseData<Boolean>, Boolean> {
	@Autowired
	private RestHighLevelClient restHighLevelClient;

	@Override
	public ResponseData<Boolean> call(RequestParam<UpdateByQueryRequest> requestParam) {
		ResponseData<Boolean> responseData = new ResponseData<>();

		UpdateByQueryRequest bulkRequest = requestParam.getRequestBody();
		try {
			BulkByScrollResponse bulkResponse = restHighLevelClient.updateByQuery(bulkRequest, RequestOptions.DEFAULT);

			System.out.println("es response data:" + bulkResponse.getStatus());
		} catch (IOException e) {
			e.printStackTrace();
			responseData = ResponseData.fail();
			responseData.setData(false);
			return responseData;
		}

		responseData = ResponseData.success();
		responseData.setData(true);

		return responseData;
	}

	@Override
	protected UpdateByQueryRequest transformRequest(OpEsIndexRequest request) {
		List<ProductDetailModel> requestList = request.getData();

		UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(request.getTableName());

		for (int i = 0; i < requestList.size(); i++) {
			ProductDetailModel requestParam = requestList.get(i);
			if(null != requestParam) {
				String id = ValueUtil.toStr(requestParam.getProductId());

				BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();

				BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery(EsConstant.ES_PRIMARY_KEY, "1"));
//					.must(QueryBuilders.matchQuery("attributes.attributeId", "1111"));

				boolBuilder.should().add(queryBuilder);

				updateByQueryRequest.setQuery(boolBuilder);
			}
		}

		return updateByQueryRequest;
	}

	@Override
	protected Boolean transformerResponse(Boolean aBoolean, RequestParam<OpEsIndexRequest> requestParam) {
		return true;
	}

}
